package srzy.module

import scala.collection.immutable.Range

object CdrComp extends AbsCaseDeal with srzy.common.Logging {

  def getIntList(s: String, c: (String, List[Int])) = {
    if (s == "") {
      List.empty[Int]
    } else {
      val len = srzy.common.split(s, """\|""").length
      logger.debug("len=" + len)

      val intl = c._1 match {
        case "+" => {
          List(0 until len: _*).filter(c._2.indexOf(_) != -1)
        }
        case "-" => {
          List(0 until len: _*).filter(c._2.indexOf(_) == -1)
        }
        case "*" => {
          List(0 until len: _*)
        }
      }
      logger.debug("intList=" + intl)
      intl
    }
  }

  def cutStr(s: String, c: List[Int]) = {

    val arrStr = srzy.common.split(s, """\|""")
    logger.debug("arrStr=" + arrStr)

    c.map(i => {
      val s = arrStr.apply(i)
      logger.debug(i + "=" + s)
      if (s.indexOf(";") == -1) {
        s
      } else {
        s.split(";").toList.sortWith(_ > _).mkString(";")
      }
    }).mkString("|")

  }

  def deal = {
    val caseType = getCaseInfo.caseType
    logger.debug("CaseType:" + getCaseInfo.caseType)
    val l: List[String] = List("Normal", "Err", "Trans")
    for (e <- l) {
      val v = typeToCdrMap.getOrElse(e, Map()).getOrElse(caseType, ("*", List()))
      logger.debug("CdrType:" + e + ",caseType:" + getCaseInfo.caseType + "para:" + v)

      e match {
        case "Normal" => {
          if (getCaseInfo.normalCdr.stdFull.length != getCaseInfo.normalCdr.realFull.length) {
            logger.debug("normalCdr.stdFull length not equal to normalCdr.realFull length")
            fail
          } else {
            val intl = getCaseInfo.normalCdr.stdFull
              .map(getIntList(_, v)).sortWith(_.length < _.length).headOption.getOrElse(List.empty[Int])
            getCaseInfo.normalCdr.stdCutted = getCaseInfo.normalCdr.stdFull.map(cutStr(_, intl))
            getCaseInfo.normalCdr.realCutted = getCaseInfo.normalCdr.realFull.map(cutStr(_, intl))
            if (getCaseInfo.normalCdr.stdCutted.sortWith(_ > _).toString
              != getCaseInfo.normalCdr.realCutted.sortWith(_ > _).toString) {
              logger.debug("cdrs are not the same")
              logger.debug("stdCuttedCdr:" + getCaseInfo.normalCdr.stdCutted)
              logger.debug("stdFullCdr:" + getCaseInfo.normalCdr.stdFull)
              logger.debug("realCuttedCdr:" + getCaseInfo.normalCdr.realCutted)
              logger.debug("realFullCdr:" + getCaseInfo.normalCdr.realFull)
              fail
            }
          }
        }

        case "Err" => {
          if (getCaseInfo.errCdr.stdFull.length != getCaseInfo.errCdr.realFull.length) {
            logger.debug("errCdr.stdFull length not equal to errCdr.realFull length")
            fail
          } else {
            val intl = getCaseInfo.errCdr.stdFull
              .map(getIntList(_, v)).sortWith(_.length < _.length).headOption.getOrElse(List.empty[Int])
            getCaseInfo.errCdr.stdCutted = getCaseInfo.errCdr.stdFull.map(cutStr(_, intl))
            getCaseInfo.errCdr.realCutted = getCaseInfo.errCdr.realFull.map(cutStr(_, intl))
            if (getCaseInfo.errCdr.stdCutted.sortWith(_ > _).toString
              != getCaseInfo.errCdr.realCutted.sortWith(_ > _).toString) {
              logger.debug("cdrs are not the same")
              logger.debug("stdCuttedCdr:" + getCaseInfo.errCdr.stdCutted)
              logger.debug("stdFullCdr:" + getCaseInfo.errCdr.stdFull)
              logger.debug("realCuttedCdr:" + getCaseInfo.errCdr.realCutted)
              logger.debug("realFullCdr:" + getCaseInfo.errCdr.realFull)
              fail
            }
          }
        }

        case "Trans" => {
          if (getCaseInfo.transCdr.stdFull.length != getCaseInfo.transCdr.realFull.length) {
            logger.debug("transCdr.stdFull length not equal to transCdr.realFull length")
            fail
          } else {
            val intl = getCaseInfo.transCdr.stdFull
              .map(getIntList(_, v)).sortWith(_.length < _.length).headOption.getOrElse(List.empty[Int])
            getCaseInfo.transCdr.stdCutted = getCaseInfo.transCdr.stdFull.map(cutStr(_, intl))
            getCaseInfo.transCdr.realCutted = getCaseInfo.transCdr.realFull.map(cutStr(_, intl))
            if (getCaseInfo.transCdr.stdCutted.sortWith(_ > _).toString
              != getCaseInfo.transCdr.realCutted.sortWith(_ > _).toString) {
              logger.debug("cdrs are not the same")
              logger.debug("stdCuttedCdr:" + getCaseInfo.transCdr.stdCutted)
              logger.debug("stdFullCdr:" + getCaseInfo.transCdr.stdFull)
              logger.debug("realCuttedCdr:" + getCaseInfo.transCdr.realCutted)
              logger.debug("realFullCdr:" + getCaseInfo.transCdr.realFull)
              fail
            }
          }
        }
      }
    }
  }

  def fail = {
    getCaseInfo.cdrCompResult = false
    getCaseInfo.caseResult = false
  }
}
